//
//  JWUpUserDataTools.m
//  见闻新闻
//
//  Created by brother on 16/3/16.
//  Copyright © 2016年 JoanBoat. All rights reserved.
//

#import "JWUpUserDataTools.h"

#import "JWUserModel.h"

#import <UIImageView+WebCache.h>

@implementation JWUpUserDataTools


+(void)updataToModel {
    
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    
    NSString *urlPath = [baseUrl stringByAppendingString:@"/User/userlogins"];
    
    JWUserModel *user = [JWUserModel userModel];
    
    
    NSDictionary *parameters1 = @{@"user_id" : user.user_id
                                  
                                  };
    
    [manger POST:urlPath parameters:parameters1 progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        
        NSUserDefaults *UserDefaults = [NSUserDefaults standardUserDefaults];
        
        [UserDefaults setObject:responseObject[@"data"] forKey:UserInfo];
        
        [UserDefaults synchronize];
        

        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
    
}



+(void)updataToUserAvatar:(UIImageView *)AvatarView HUDView: (UIView *)HUD {

    JWUserModel *user = [JWUserModel userModel];
    
    NSURL *url = [NSURL URLWithString: user.user_icon];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:HUD animated:YES];
 
    [AvatarView sd_setImageWithURL:url placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        
        
        hud.mode =  MBProgressHUDModeAnnularDeterminate;
        
       hud.labelText = @"正在更新用户头像";
        
        hud.progress = (float)receivedSize/(float)expectedSize;
        
        
       hud.detailsLabelText = [NSString stringWithFormat:@"%.2f%%",hud.progress*100];

        
        hud.activityIndicatorColor  = [UIColor colorWithRed:13/255.0 green:184/255.0 blue:246/255.0 alpha:1];
        
        hud.detailsLabelColor = [UIColor whiteColor];
        

        
        hud.detailsLabelFont = [UIFont fontWithName:@"TimesNewRomanPSMT" size:18];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           hud.hidden = YES;
            
            
            UIAlertView *MesgVW = [[UIAlertView alloc]initWithTitle:@"提示" message:@"服务器正忙  更新失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [MesgVW removeFromSuperview];
                
            });
            
            
        });
        
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        [hud  hide:YES];
        //[MBProgressHUD hideHUDForView:self.view animated:YES];
        
    }];



}



@end
